class Solution(object):
    def reorderedPowerOf2(self, n):
        n = str(n)
        m = len(n)
        p = [0] * m
        out = []

        def judge():
            num = int(''.join(out))
            return not num & (num - 1)

        def search(i):
            if i == m:
                return judge()
            else:
                if i == 0:
                    for j in range(m):
                        if n[j] != '0':
                            out.append(n[j])
                            p[j] = 1
                            flag = search(i + 1)
                            out.pop()
                            p[j] = 0
                            if flag:
                                return True
                else:
                    for j in range(m):
                        if not p[j]:
                            out.append(n[j])
                            p[j] = 1
                            flag = search(i + 1)
                            out.pop()
                            p[j] = 0
                            if flag:
                                return True
                return False
        return search(0)


